package
{
	import com.imcrl.component.ext.ButtonImage;
	import com.imcrl.core.state.Scene;
	import com.imcrl.core.state.SceneMachine;
	import com.imcrl.geom.Padding;
	
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	import flash.utils.setTimeout;
	
	import starling.core.Starling;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.KeyboardEvent;
	import starling.events.ResizeEvent;
	import starling.textures.Texture;
	
	import starlingui.component.drag.AbsIcon;
	import starlingui.component.event.DesktopEvent;
	import starlingui.state.DesktopScene;
	import starlingui.state.SinglerScene;
	import starlingui.state.WellcomeScene;
	
	public class Application extends Sprite
	{
		public static const WIDTH:int=647;
		public static const HEIGHT:int=434;
		
		public static const BACKGROUND_W:int=1000;
		public static const BACKGROUND_H:int=512;
		public static const padding:Padding=new Padding(35,70,0,178);
		public static var instance:Application;
		private var scene:SceneMachine;
		private var background:Background;
		public var iphoneContainer:Sprite;
		private var iphoneBackground:AbsIcon;
		public function Application()
		{
			super();
		
			instance=this;
			this.addEventListener(Event.ADDED_TO_STAGE,stageHandle);
		}
		
		private function stageHandle(evemt:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE,stageHandle);
			
			iphoneContainer=new Sprite();
			
			var innerIphoneContainer:Sprite=new Sprite();
			innerIphoneContainer.x=padding.left;
			innerIphoneContainer.y=padding.top;
			
			scene=new SceneMachine(innerIphoneContainer);
			scene.add(new WellcomeScene());
			scene.add(new DesktopScene());
			scene.add(new SinglerScene());
			
			scene.state=WellcomeScene.NAME;
			
			iphoneBackground=new AbsIcon(Texture.empty(32,32,0x0),false);
			iphoneBackground.touchable=false;
			iphoneContainer.addChild(iphoneBackground);
			iphoneContainer.addChild(innerIphoneContainer);

			var texture:Texture=Texture.empty(100,100,0x00FFFFFF);
			var triggerBtn:ButtonImage=new ButtonImage(texture);
			triggerBtn.alpha=0.3;
			triggerBtn.addEventListener(Event.TRIGGERED,triggeredHandle);
			triggerBtn.x=860;
			triggerBtn.y=200;
			iphoneContainer.addChild(triggerBtn);
			
			
			background=new Background();
			background.touchable=false;
			this.addChild(background);
			this.addChild(iphoneContainer);
			
			
			var t:int=2000;
			setTimeout(loadBackground,t);
			stage.addEventListener(ResizeEvent.RESIZE,resizeHandle);
			
			var w:int=stage.stageWidth;
			var h:int=stage.stageHeight;
			background.resize(w,h);
			iphoneContainer.x=(w-BACKGROUND_W)>>1;
			iphoneContainer.y=(h-BACKGROUND_H)>>1;
			
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyHandle);
		}
		
		private function loadBackground():void{
			iphoneBackground.load("assets/iphone.png");
			var v:int=Math.random()*5;
			background.load("assets/noise"+v+".jpg");
		}
		
		private function triggeredHandle(event:Event):void{
			var scene:Scene=this.scene.getState(this.scene.state) as Scene;
			if(scene){
				if(scene.hasEventListener(DesktopEvent.EXIT)){
					scene.dispatchEvent(new DesktopEvent(DesktopEvent.EXIT));
				}
			}
		}
		
		/**
		 * 退出操作; 
		 * @param event
		 * 
		 */		
		private function keyHandle(event:KeyboardEvent):void{
			if(event.keyCode==Keyboard.ESCAPE){
				triggeredHandle(event);
			}
		}
		
		private function resizeHandle(event:ResizeEvent):void{
			
			var w:int=event.width;
			var h:int=event.height;
			
			stage.stageWidth=w;
			stage.stageHeight=h;
			Starling.current.viewPort=new Rectangle(0,0,w,h);
			
			background.resize(w,h);

			iphoneContainer.x=(w-BACKGROUND_W)>>1;
			iphoneContainer.y=(h-BACKGROUND_H)>>1;
		}
		
		
	}
}